home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / fbm12s.lha / fbgamma.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-18  |  4.1 KB  |  169 lines

  1. /*****************************************************************
  2.  * fbgamma.c: FBM Release 1.2 07-Apr-93 Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989-1993 by Michael Mauldin.  Permission is granted
  5.  * to use this file in whole or in part for any purpose, educational,
  6.  * recreational or commercial, provided that this copyright notice
  7.  * is retained unchanged.  This software is available to all free of
  8.  * charge by anonymous FTP and in the UUNET archives.
  9.  *
  10.  * fbgamma.c: Apply a gamma transform to a colormapped image
  11.  *
  12.  * USAGE
  13.  *     % fbgamma [ -<type> ] [ amount ] < image1 > image2
  14.  *
  15.  * EDITLOG
  16.  *     LastEditDate = Mon Jun 25 00:02:30 1990 - Michael Mauldin
  17.  *     LastFileName = /usr2/mlm/src/misc/fbm/fbgamma.c
  18.  *
  19.  * HISTORY
  20.  * 07-Apr-93  Michael Mauldin (mlm) at Carnegie-Mellon University
  21.  *    Added -J switch
  22.  *
  23.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  24.  *    Package for Release 1.0
  25.  *
  26.  * 05-Apr-89  Michael Mauldin (mlm) at Carnegie-Mellon University
  27.  *     Created.
  28.  *****************************************************************/
  29.  
  30. # include <stdio.h>
  31. # include <math.h>
  32. # include "fbm.h"
  33.  
  34. # define USAGE \
  35.   "Usage: fbgamma [ -<type> ] [ amount ] < image > image"
  36.  
  37. #ifndef lint
  38. static char *fbmid =
  39. "$FBM fbgamma.c <1.2> 07-Apr-93 (C) 1989-1993 by Michael Mauldin, source \
  40. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  41. #endif
  42.  
  43. int debug = 0;
  44. double expon = 1.0;
  45.  
  46. main (argc, argv)
  47. char *argv[];
  48. { FBM image;
  49.   int outtype = DEF_8BIT;
  50.   double amount = 2.2, atof ();
  51.   char *fname = NULL;
  52.  
  53.   /* Get the options */
  54.   while (--argc > 0 && (*++argv)[0] == '-')
  55.   { while (*++(*argv))
  56.     { switch (**argv)
  57.       {
  58.     case 'd':    debug++; break;
  59.     case 'A':    outtype = FMT_ATK; break;
  60.     case 'B':    outtype = FMT_FACE; break;
  61.     case 'F':    outtype = FMT_FBM; break;
  62.     case 'G':    outtype = FMT_GIF; break;
  63.     case 'I':    outtype = FMT_IFF; break;
  64.     case 'J':    outtype = FMT_JPEG; break;
  65.     case 'L':    outtype = FMT_LEAF; break;
  66.     case 'M':    outtype = FMT_MCP; break;
  67.     case 'P':    outtype = FMT_PBM; break;
  68.     case 'R':    outtype = FMT_RLE; break;
  69.     case 'S':    outtype = FMT_SUN; break;
  70.     case 'T':    outtype = FMT_TIFF; break;
  71.     case 'X':    outtype = FMT_X11; break;
  72.     case 'Z':    outtype = FMT_PCX; break;
  73.     default:        fprintf (stderr, "%s\n", USAGE);
  74.                         exit (1);
  75.       }
  76.     }
  77.   }
  78.  
  79.   if (argc > 0)
  80.   { amount = atof (argv[0]); }
  81.  
  82.   if (argc > 1)
  83.   { fname = argv[1]; }
  84.  
  85.   /* Clear the memory pointers so alloc_fbm won't be confused */
  86.   image.cm  = image.bm  = (unsigned char *) NULL;
  87.  
  88.   /* Read the image */
  89.   if (read_bitmap (&image, fname))
  90.   { int h, w, k;
  91.  
  92.     /* Determine output height & width (oh*ow <= size) */
  93.     h = image.hdr.rows;
  94.     w = image.hdr.cols;
  95.     k = image.hdr.planes;
  96.  
  97.     fprintf (stderr,
  98.          "Gamma correct \"%s\", gamma %1.3lf [%dx%dx%d]\n",
  99.          image.hdr.title[0] ? image.hdr.title : "(untitled)",
  100.          amount, w, h, k);
  101.  
  102.     if (gamma_fbm (&image, amount) &&
  103.         write_bitmap (&image, stdout, outtype))
  104.     { exit (0); }
  105.   }
  106.  
  107.   exit (1);
  108. }
  109.  
  110. gamma_fbm (image, amount)
  111. FBM *image;
  112. double amount;
  113. { double clr;
  114.   register int i, w, h, k;
  115.   register unsigned char *bmp, *btail;
  116.   int cmap[256];
  117.  
  118.   /* Determine output height & width (oh*ow <= size) */
  119.   h = image->hdr.rows;
  120.   w = image->hdr.cols;
  121.   k = image->hdr.planes;
  122.  
  123.   expon = 1.0 / amount;
  124.  
  125.   if (image->hdr.clrlen > 0)
  126.   {
  127.     for (i=0; i<image->hdr.clrlen; i++)
  128.     {
  129.       if (debug) fprintf (stderr, "value %3d => ", image->cm[i]);
  130.       image->cm[i] = gamma_clr (image->cm[i]);
  131.       if (debug) fprintf (stderr, "%3d\n", image->cm[i]);
  132.     }
  133.   }
  134.   else
  135.   {
  136.     for (i=0; i<256; i++)
  137.     { cmap[i] = gamma_clr (i);
  138.  
  139.       if (debug) fprintf (stderr, "value %3d => %3d\n", i, cmap[i]);
  140.     }
  141.     
  142.     bmp = image->bm;
  143.     btail = bmp + image->hdr.plnlen * image->hdr.planes;
  144.     
  145.     while (bmp < btail)
  146.     { *bmp = cmap[*bmp]; bmp++; }
  147.   }
  148.  
  149.   
  150.   return (1);
  151. }
  152.  
  153. int gamma_clr (value)
  154. int value;
  155. { double clr, floor ();
  156.   int newclr;
  157.  
  158.   /* First compute new color */
  159.   clr = 255.0 * pow ((value / 255.0), expon);
  160.  
  161.   /* Round to nearest integer */
  162.   newclr = (int) floor (clr + 0.5);
  163.   
  164.   if (newclr < 0) return (0);
  165.   if (newclr > 255) return (255);
  166.   
  167.   return (newclr);
  168. }
  169.